<?php
class Silk_Zgb_Helper_Data extends Mage_Core_Helper_Abstract
{	
	const MANUFACTURER = 3;
	const DEALER = 4;
	const WAREHOUSE = 5;
	const GROUP = 218;

	const MANUFACTURER_ID = 135;
	const DEALER_ID = 138;
	const WAREHOUSE_ID = 137;

	public function getRootCategoryId()
	{
		return 4;
	}
	public function getUserRoles()
	{
		return array(3,4,5);
	}

	public function getRoleId($role)
	{
		$roleId = 0;
		switch($role)
		{
			case 'manufacturer':
				$roleId = 3;
				break;
			case 'dealer':
				$roleId = 4;
				break;
			case 'warehouse':
				$roleId = 5;
				break;
		}
		if($roleId) return $roleId;
		throw new Exception("Role is not found!");
	}

	public function getWebsiteBySku()
	{

	}

	//根据dealerID获取SKU所属的websiteid
	public function getDealerWebsiteBySku($dealerId,$id)
	{
		$attribute_id = self::DEALER_ID;

		$sql = "SELECT DISTINCT(store_id) FROM `catalog_product_entity_int` WHERE store_id!=0 AND entity_id='{$id}' AND attribute_id='{$attribute_id}' AND `value`='{$dealerId}'";

		$resource = Mage::getSingleton('core/resource');
		$db = $resource->getConnection('core_read');

		$query = $db->query($sql);
		$items = $query->fetchAll();
		$websites = array();

		foreach($items as $item)
		{
			$website = Mage::getModel('core/website')->load($item['store_id']);
			if($website && $website->getId())
			{
				$websites[] = $website->getId();
			}
		}
		return $websites;
	}

	public function reindexCategoryProduct($storeId, $product, $data)
	{
		return;
		if($storeId && $product && $data)
		{
			$resource = Mage::getSingleton('core/resource');
			$read = $resource->getConnection('core_read');
			$write = $resource->getConnection('core_write');
			$id = $product->getId();
			$sql = "SELECT * FROM `catalog_product_website` AS cpw  LEFT JOIN `catalog_category_product` AS ccp ON cpw.product_id=ccp.product_id WHERE cpw.product_id='{$id}'";
			echo $sql;
			$query = $read->query($sql);
			$items = $query->fetchAll();
			$query = null;

			foreach($items as $item)
			{
				$sql = "SELECT COUNT(*) from `catalog_category_product_index` WHERE category_id='{$item['category_id']}' AND product_id='{$item['product_id']}' AND store_id='{$item['store_id']}'";
				echo $sql;
				$query = $read->query($sql);
				$total = $query->fetchColumn();
				if(!$total)
				{
					$sql = "INSERT INTO `catalog_category_product_index` SET category_id='{$item['category_id']}',product_id='{$item['product_id']}',`position`='{$item['position']}',is_parent=0,store_id='{$item['store_id']}',visibility='{$data['visibility']}'";
					echo $sql;
				}
			}
		}
	}

	public function getSaleTagUrl($_product, $skinUrl)
	{
		$tags = array(
			'促销' => $skinUrl.'/cuxiao.png',
			'爆款' => $skinUrl.'/baokuan.png',
			'新品' => $skinUrl.'/xinpin.png',
			'热卖' => $skinUrl.'/remai.png'
			);
		$tag = trim($_product->getAttributeText('tag'));

		if(!$tag) return false;
		
		return $tags[$tag];
	}

	public function hasTag($_product)
	{
		$tag = $_product->getAttributeText('tag');

		if($tag){
			return true;
		}
		return false;
	}

	public function getSubCategorys()
	{
		$model = Mage::getModel('catalog/category');
		$model->load(12);
		$collection = $model->getChildrenCategories();

		$options = array();
		
		foreach($collection as $category)
		{
			$options[$category->getId()] = $category->getName();
		}
		return $options;
	}

	public function getProductInfoTags($_product)
	{
		$tags = $_product->getProductInfoTag();
		if($tags){
			return explode('|', $tags);
		}else{
			return false;
		}
	}

}
	 